/**
 * Created by joy on 12/09/2017.
 */
import EthRpcHelper from '../../common/helpers/chain/EthRpcHelper'
import EthWalletHelper from '../../common/helpers/chain/EthWalletHelper'
import CommonUtil from '../../common/utils/CommonUtil'
import prompt from 'prompt'


export default async () => {
  const schema = {
    properties: {
      host: {
        description: 'Enter host of node',
        type: 'string',
        required: true,
        default: '127.0.0.1'
      },
      port: {
        description: 'Enter port of node',
        type: 'number',
        required: true,
        default: 8545
      },
      eventName: {
        description: 'Enter event name',
        type: 'string',
        required: true,
        default: 'Transfer'
      },
      indexes: {
        description: 'Enter indexes',
        type: 'string',
        required: true,
        default: '{"from": "0x00C8C8551d5Ba534D82fFeF87226DC0724eb226b"}'
      },
      contractAddress: {
        description: 'Enter contractAddress',
        type: 'string',
        required: true,
        default: '0xa087c1841a15a82E0dBd05cE093ea52319340520'
      },
      abiJson: {
        description: 'Enter abiJson',
        type: 'string',
        required: true,
        default: '[{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":false,"stateMutability":"nonpayable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}]'
      },
    }
  }

  const args = await CommonUtil.getArgsFromConsole(prompt, schema)
  let { host, port, eventName, indexes, contractAddress, abiJson } = args
  const ethRpcHelper = new EthRpcHelper(host, port)
  const eventInstance = await ethRpcHelper.getEventInstance(contractAddress, JSON.parse(abiJson), eventName, JSON.parse(indexes))
  eventInstance.watch((err, result) => {
    if (err) {
      logger.error(err)
    } else {
      logger.info(result)
    }
  })
}
